package com.qiang.pagerindicatordemo;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qiang.viewpagerindicator.CirclePageIndicator;
import com.qiang.viewpagerindicator.LinePageIndicator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;
    LinePageIndicator pageIndicator;
    CirclePageIndicator circleIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        viewPager = (ViewPager) findViewById(R.id.viewPager);
        pageIndicator = (LinePageIndicator) findViewById(R.id.pageIndicator);
        circleIndicator = (CirclePageIndicator) findViewById(R.id.circleIndicator);

        viewPager.setAdapter(new PageAdapter());
        circleIndicator.setViewPager(viewPager);
    }

    class PageAdapter extends PagerAdapter {

        List<View> mList;

        public PageAdapter() {
            mList = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_pager, null);
                TextView text = (TextView) view.findViewById(R.id.text);
                text.setText(String.valueOf(i));
                mList.add(view);
            }
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mList.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = mList.get(position);
            container.removeView(view);
        }

    }

}
